﻿/*
Copyright (c) 2010 Lance Ward
This software is licensed under the MIT license, except where otherwise noted.
For complete license details, refer to license.txt
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Drawing;
using System.Xml.Serialization;
using ADNLib;

namespace ADN
{
	public class FileManager : IDisposable
	{        
        private string fileName = String.Empty;
        private string tempFileName = String.Empty;
        const int saveInterval = 5000;    //every five minutes
        private Timer saveTimer = null;
        private Animation movie = null;
        private bool needsSave = false;

        public FileManager()
        {
            Initialize();
        }

        #region Properties

        public Animation Movie
        {
            get { return movie; }
        }

        public bool NeedsSave
        {
            get { return needsSave; }
            set { needsSave = value; }
        }

		public string FileName
		{
			get { return fileName; }
			set { fileName = value; }
		}

        #endregion

        #region Public Methods

        public void Dispose()
        {
            saveTimer.Stop();
            saveTimer.Dispose();

            //close any open files
        }

        public void Save()
        {
            if (movie != null)
            {
                if (String.IsNullOrEmpty(fileName))
                {
                    SaveAs();
                }
                else
                {
                    AnimIO.Save(fileName, movie);
                    needsSave = false;
                }
            }
        }

        public void SaveAs()
        {
            if (movie != null)
            {
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.Filter = "ADN Files|*.adnx";
                sfd.InitialDirectory = Properties.Settings.Default.DefaultProjectDirectory;

                DialogResult res = sfd.ShowDialog();

                if (res != DialogResult.Cancel)
                {
                    fileName = sfd.FileName;
                    Properties.Settings.Default.DefaultProjectDirectory =
                        Path.GetDirectoryName(sfd.FileName);

                    AnimIO.Save(fileName, movie);
                    needsSave = false;
                }
            }
        }

        public void Open()
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "ADN Files|*.adnx";
            ofd.CheckFileExists = true;
            ofd.Multiselect = false;
            ofd.InitialDirectory = Properties.Settings.Default.DefaultProjectDirectory;

            DialogResult res = ofd.ShowDialog();

            if (res != DialogResult.Cancel)
            {
                fileName = ofd.FileName;
                Properties.Settings.Default.DefaultProjectDirectory = 
                    Path.GetDirectoryName(ofd.FileName);

                movie = AnimIO.Load(fileName);
                if (String.IsNullOrEmpty(tempFileName))
                {
                    tempFileName = Path.GetTempFileName();
                }
                AnimIO.Save(tempFileName, movie);
            }

        }

        //create a new movie
        public void Create(Size resolution, int fps)
        {
            if (movie != null && !needsSave)
            {
                DialogResult res = MessageBox.Show("Would you like to save changes to the current document", "Save changes", MessageBoxButtons.YesNo);
                if (res == DialogResult.Yes) { Save(); }
            }

			tempFileName = Path.Combine(Application.StartupPath, "temp/tempfile.tmp");
            movie = new Animation();
            movie.Resolution = resolution;
            movie.FPS = fps;
            Layer initLayer = new Layer();
            Frame initFrame = new Frame(0, 1);
            initLayer.Frames.Add(initFrame);
            movie.Layers.Add(initLayer);
       
            AnimIO.Save(tempFileName, movie);
            needsSave = true;
        }

        public void Create()
        { Create(new Size(4000, 3000), 25); }

        #endregion

        private void Initialize()
        {
            saveTimer = new Timer();
            saveTimer.Interval = saveInterval;
            saveTimer.Tick += new EventHandler(saveTimer_Tick);
            saveTimer.Start();
        }

        void saveTimer_Tick(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(tempFileName) && movie != null)
            {
                AnimIO.Save(tempFileName, movie);
            }
        }
	}
}
